package com.tz.cartelo.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.tz.cartelo.BaseApplication;
import com.tz.cartelo.R;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.result.ResultGoodsClassifySearch;
import com.tz.cartelo.view.AutoLinefeedLayout;
import com.tz.common.util.LogUtil;

/**
 * Created by Administrator on 2017/4/16.
 */

public class GoodsClassifySearchActivity extends DataLoadActivity implements View.OnClickListener {
    private static final String TAG = "Activity";
    private Api mApi;
    private EditText mGoodsClassifySearchEdit;
    private AutoLinefeedLayout goods_classify_search_layout;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_goods_classify_search;
    }

    @Override
    protected void init() {
        BaseApplication.addActivity(this);

        mGoodsClassifySearchEdit = (EditText) findViewById(R.id.goods_classify_search_edit);
        goods_classify_search_layout = (AutoLinefeedLayout) findViewById(R.id.goods_classify_search_layout);
        findViewById(R.id.goods_classify_cancel).setOnClickListener(this);
        mGoodsClassifySearchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if (mGoodsClassifySearchEdit.getText().toString().trim().isEmpty()) {
                        showToast(R.string.goods_classify_search_empty);
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString(Key.GOODS_CLASSIFY_TITLE, mGoodsClassifySearchEdit.getText().toString().trim());
                        switchActivity(GoodsClassifyDetailsActivity.class, bundle, Key.SEARCH_GOODS_NAME);
                    }
                }
                return true;
            }
        });

        mApi = Api.GOODS_CLASSIFY_SEARCH;
        loadData(mApi, true);
    }

    @Override
    protected void initTitle() {
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goods_classify_cancel:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initParams(DataParams params) {
    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case GOODS_CLASSIFY_SEARCH:
                final ResultGoodsClassifySearch resultGoodsClassifySearch = fromJson(response, ResultGoodsClassifySearch.class);
                if (!resultGoodsClassifySearch.isSuccess()) {
                    showToast(resultGoodsClassifySearch.getMessage());
                    return;
                }

                String[] name = new String[resultGoodsClassifySearch.getGoodsClassifySearch().getGoodsCategoryTypesList().size()];

                for (int i = 0; i < name.length; i++) {
                    name[i] = resultGoodsClassifySearch.getGoodsClassifySearch().getGoodsCategoryTypesList().get(i).getTitle();
                    View view = LayoutInflater.from(this).inflate(R.layout.item_goods_classify_search_text, null);
                    TextView tValue = (TextView) view.findViewById(R.id.item_search_text);
                    tValue.setText(name[i]);
                    goods_classify_search_layout.addView(view);

                    final int finalI = i;
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Bundle bundle = new Bundle();
                            bundle.putString(Key.GOODS_CLASSIFY_TITLE, resultGoodsClassifySearch.getGoodsClassifySearch().getGoodsCategoryTypesList().get(finalI).getTitle());
                            switchActivity(GoodsClassifyDetailsActivity.class, bundle, Key.SEARCH_GOODS_NAME);
                        }
                    });
                }
                break;
            default:
                break;
        }
    }


}
